#!/usr/bin/python
# -*- coding: utf-8 -*-
#coding=utf-8

import datetimeDemo as tsLog

def demo():
    tsLog.logTs("arrayDemo", preNewLineNum=2)
    month = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"]

    # for-in 循环
    for name in month:
        print(name)

    # 下标循环
    for index in range(len(month)):
        print(index, month[index])
    for index in range(0, len(month), 2):
        print("even, ", index, month[index])
    for index in range(1, len(month), 2):
        print("odd, ", index, month[index])

    # enumerate python的内置函数, 入参为可遍历/可迭代的对象(如列表、字符串)，利用它可以同时获得索引和值
    for index, value in enumerate(month):
        print("enumerate", index, value)

    print("list:", month)

    # 升序排序 可迭代对象（例如元组、列表、字典等）进行排序，返回排好序的对象，不改变数组自身
    print("after sorted, list:", sorted(month))
    # 降序排序 可迭代对象（例如元组、列表、字典等）进行排序，返回排好序的对象，不改变数组自身
    print("after sorted reverse, list:", sorted(month, reverse=True))
    # 按照单词的第二个字母 降序排序
    print("after sorted key=x[1], list:       ", sorted(month, key=lambda x:x[1], reverse=True))
    # 按照单词的第二个字母 和 第三个字母排序 降序排序
    print("after sorted key=(x[1],x[2]), list:", sorted(month, key=lambda x:(x[1],x[2]), reverse=True))

    # sort 将改变数组自身
    month.sort()
    print("after sort, list:                 ", month)
    month.sort(key=lambda x:x[1], reverse=True)
    print("after sort key=x[1] reverse, list:", month)

if __name__ == '__main__':
    demo()